<template>
  <div
    :class="className"
    class="flex flex-1"
    :style="{ height: height, width: width }"
  />
</template>

<script>
import * as echarts from "echarts";
import resize from "@/views/dashboard/mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "100%"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    // 接收配置项
    chartOption: {
      type: Object,
      required: false
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    chartOption: {
      deep: true,
      handler(val) {
        // debugger
        // console.log("配置项已更新");
        this.setOptions();
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions();
    },
    setOptions() {
      // console.log('this.chartOption',this.chartOption);
      if (this.chartOption) {
        this.chart.setOption(this.chartOption, true);
      }
    }
  }
};
</script>
